﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using Model;
using ZedGraph;

public partial class StatisticData_PackageTimeStatistic_PackageStatisticbyAllPID_DT : System.Web.UI.Page
{
    private string pIDs
    {
        get { return ViewState["pIDs"].ToString(); }
        set { ViewState["pIDs"] = value; }
    }
    private DateTime dt1
    {
        get { return DateTime.Parse(ViewState["starttime"].ToString()); }
        set { ViewState["starttime"] = value; }
    }
    private DateTime dt2
    {
        get { return DateTime.Parse(ViewState["stoptime"].ToString()); }
        set { ViewState["stoptime"] = value; }
    }
    private Boolean Isbarchart
    {
        get { return bool.Parse(ViewState["Isbarchart"].ToString()); }
        set { ViewState["Isbarchart"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Page.Unload += new EventHandler(Page_Unload);
            BindProductType();            
        }
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
    }

    protected void BindProductType()
    {
        ProductType pro = new ProductType();
        foreach (ProductTypeInfo proinfo in pro.LoadEntities())
            this.CheckBoxList1.Items.Add(proinfo.ProductTypeID);
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        pIDs = getPIDs();
        dt1 = DateTime.Parse(this.TextBox1.Text.Trim());
        dt2 = DateTime.Parse(this.TextBox2.Text.Trim());
        Bind();
        Isbarchart = true;
        this.CrystalReportViewer1.Visible = true;
        this.CrystalReportViewer2.Visible = false;
    }

    protected void Bind()
    {
        if (pIDs == "")
        {
            string str = "请选择产品类型！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
        }
        else
        {
            //用GridView与ZedGraph展现的示例
            //BindGV();
            //this.ZedGraphWeb1.Visible = true;

            //用水晶报表展现的示例
            BindQCR();
            BindQCR1();
        }
    }

    protected void BindGV()
    {
        PackageProduct_QT opr = new PackageProduct_QT();
        this.GridView1.DataSource = opr.GetAllPID_QT1(pIDs, dt1, dt2).Tables[0];
        this.GridView1.DataBind();
        this.GridView1.Visible = true;
    }

    protected string getPIDs()
    {
        string s = "";
        foreach (ListItem item in this.CheckBoxList1.Items)
        {
            if (item.Selected)
                s += item.Text + ",";
        }
        if (s.Length == 0)
            return "";
        s = s.Substring(0, s.Length - 1);        
        return s;        
    }

    private void BindQCR()
    {
        this.CrystalReportSource1.ReportDocument.Load(Server.MapPath("PackageStatisticbyAllPID_DT.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<ProductType_QInfo> list = GetQData();
        this.CrystalReportSource1.ReportDocument.SetDataSource(list);
        this.CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        this.CrystalReportViewer1.DataBind();
    }

    private void BindQCR1()
    {
        this.CrystalReportSource2.ReportDocument.Load(Server.MapPath("PackageStatisticbyAllPID_DT1.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource2.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<ProductType_QInfo> list = GetQData();
        this.CrystalReportSource2.ReportDocument.SetDataSource(list);
        this.CrystalReportSource2.DataBind();
        this.CrystalReportViewer2.ReportSource = CrystalReportSource2;
        this.CrystalReportViewer2.DataBind();
    }

    private IList<ProductType_QInfo> GetQData()
    {
        IList<ProductType_QInfo> oprs = new List<ProductType_QInfo>();
        PackageProduct_QT opr = new PackageProduct_QT();
        oprs = opr.GetAllPID_QT(pIDs, dt1, dt2);
        return oprs;
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        Bind();
        if (Isbarchart == true)
        {
            this.CrystalReportViewer1.Visible = false;
            this.CrystalReportViewer2.Visible = true;
            Isbarchart = false;
        }
        else
        {
            this.CrystalReportViewer1.Visible = true;
            this.CrystalReportViewer2.Visible = false;
            Isbarchart = true;
        }
    }

    protected void ZedGraphWeb1_RenderGraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, ZedGraph.MasterPane pane)
    {
        PackageProduct_QT t_qt = new PackageProduct_QT();
        DataSet t_qs = t_qt.GetAllPID_QT1(pIDs, dt1, dt2);

        GraphPane myPane = pane[0];

        myPane.Title.Text = "包装量/日历日&产品类型";
        myPane.XAxis.Title.Text = "日历日";
        myPane.YAxis.Title.Text = "包装量";

        //创建曲线

        PointPairList[] list = new PointPairList[t_qs.Tables[0].Columns.Count - 1];
        for (int i = 0; i < t_qs.Tables[0].Columns.Count-1 ; i++)
        {
            list[i] = new PointPairList();
        }
        for (int x = 0; x < t_qs.Tables[0].Rows.Count; x++)         //第一列为日历日数组，用作X轴的Label，数据从第二列开始
        {
            for (int j = 0; j < t_qs.Tables[0].Columns.Count - 1; j++)
            {
                double y = Convert.ToDouble((t_qs.Tables[0].Rows[x][j+1].ToString()=="") ? "0" : t_qs.Tables[0].Rows[x][j+1].ToString());
                list[j].Add(x, y);
            }
        }   

        RandomColor rc=new RandomColor();
        Color[] rcs = rc.GetRandomColorArray(t_qs.Tables[0].Columns.Count - 1);
        for (int i = 0; i < t_qs.Tables[0].Columns.Count - 1; i++)
        {
            myPane.AddBar(t_qs.Tables[0].Columns[i + 1].Caption, list[i], rcs[i]);//增加一个Bar,List里面是不是包含了（X，Y)
        }

        string[] labels = new string[t_qs.Tables[0].Rows.Count];
        for (int i = 0; i < t_qs.Tables[0].Rows.Count; i++)  //这个循环主要是取到里面的说明文字，用了一个数组的方法
        {
            labels[i] = t_qs.Tables[0].Rows[i][0].ToString();
        }

        myPane.XAxis.Scale.TextLabels = labels; //这里设置的是TextLabels属性，所以下面x轴的类型必须也设置为Text   

        //设置x轴的样式
        myPane.XAxis.Type = AxisType.Text;
        MyGraphStyle.BuildbarGraphStyle(myPane);
        pane.AxisChange(g);
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindGV();
    }
}
